﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using BusinessLayer.Model.Services.Abstract;
using BusinessLayer.Model.Repositories.Abstract;
using BusinessLayer.Model.Entities;
using System.Web.Security;
using BusinessLayer;
using DataAccess.Repositories;

namespace DataAccess.Services.Abstract
{
    /// <summary>
    /// Serivio de aplicación
    /// </summary>
    public class ApplicationService : IApplicationService
    {
        /// <summary>
        /// Obtiene o define el folder de la aplicación
        /// </summary>
        public string ApplicationFolder
        {
            get { return (string)HttpContext.Current.Application["ApplicationFolder"]; }
            set { HttpContext.Current.Application["ApplicationFolder"] = value; }
        }

        /// <summary>
        /// Obtiene o define el folder de los archivos de datos
        /// </summary>
        public string DataFolder
        {
            get { return (string)HttpContext.Current.Application["DataFolder"]; }
            set { HttpContext.Current.Application["DataFolder"] = value; }
        }

        /// <summary>
        /// Obtiene o define el folder que contiene los archivos xml.
        /// </summary>
        public string XmlFilesFolder
        {
            get { return (string)HttpContext.Current.Application["XmlFilesFolder"]; }
            set { HttpContext.Current.Application["XmlFilesFolder"] = value; }
        }

        /// <summary>
        /// Obtiene o define el folder de los archivos de noticias
        /// </summary>
        public string NewsFilesFolder
        {
            get { return (string)HttpContext.Current.Application["NewsFilesFolder"]; }
            set { HttpContext.Current.Application["NewsFilesFolder"] = value; }
        }

        /// <summary>
        /// Obtiene o define el folder de los archivos de log
        /// </summary>
        public string LogsFolder
        {
            get { return (string)HttpContext.Current.Application["LogsFolder"]; }
            set { HttpContext.Current.Application["LogsFolder"] = value; }
        }

        /// <summary>
        /// Obtiene o define el folder de la galeria de imágenes 
        /// </summary>
        public string GalleryFolder
        {
            get { return (string)HttpContext.Current.Application["GalleryFolder"]; }
            set { HttpContext.Current.Application["GalleryFolder"] = value; }
        }

        /// <summary>
        /// Url de la página
        /// </summary>
        public string ApplicationURL
        {
            get { return (string)HttpContext.Current.Application["ApplicationURL"]; }
            set { HttpContext.Current.Application["ApplicationURL"] = value; }
        }

        /// <summary>
        /// Obtiene o define el folder de los banners
        /// </summary>
        public string BannerFolder
        {
            get { return (string)HttpContext.Current.Application["BannerFolder"]; }
            set { HttpContext.Current.Application["BannerFolder"] = value; }
        }

        /// <summary>
        /// Genera todos los folders de la aplicación si hay alguno inválido
        /// </summary>
        public void GenerateSystemFoldersIfInvalid()
        {
            /*if (!Directory.Exists(DataFolder))
                Directory.CreateDirectory(DataFolder);

            if (!Directory.Exists(XmlFilesFolder))
                Directory.CreateDirectory(XmlFilesFolder);

            if (!Directory.Exists(NewsFilesFolder))
                Directory.CreateDirectory(NewsFilesFolder);

            if (!Directory.Exists(LogsFolder))
                Directory.CreateDirectory(LogsFolder);

            if (!Directory.Exists(GalleryFolder))
                Directory.CreateDirectory(GalleryFolder);

            if (!Directory.Exists(BannerFolder))
                Directory.CreateDirectory(BannerFolder);
            */
            //chequear si la cuenta de usuarios está.

            IAccountRepository accountRepository = new AccountRepository();

            if (!accountRepository.AccountExists())
            {               
                Account a = new Account
                {
                    Username = "admin",
                    Password = FormsAuthentication.HashPasswordForStoringInConfigFile("admin", Constants.EncryptionMethod)
                };

                accountRepository.Add(a);
                accountRepository.SubmitChanges();
            }

        }

        /// <summary>
        /// Genera todos los folders de la aplicación si hay alguno inválido
        /// </summary>
        public string GetRelativePath(string path, HttpRequest request)
        {
            string relativePath = path.Replace(request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty);

            string appPath = (request.ApplicationPath.Substring(request.ApplicationPath.Length - 1) == "/") ?
                request.ApplicationPath : request.ApplicationPath + "/";

            return request.Url.Scheme + 
                   "://" + request.Url.Authority + 
                   appPath + 
                   relativePath.Replace("\\", "/");

        }

    }
}
